//
//  ZLExamResultViewController.m
//  ZLExamination
//
//  Created by Zheng on 2017/8/10.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLExamResultViewController.h"

#import "ZLExamResultView.h"
#import "ZLExamResultCell.h"
#import "ZLExamResultModel.h"
#import "ZLExamBeginViewController.h"

#import "ZLWrongParsingViewController.h"

@interface ZLExamResultViewController ()

@property (nonatomic, strong) ZLExamResultView *resultView;
@property (nonatomic, strong) UIView *footerView;
@property (nonatomic, strong) ZLExamResultModel *model;

@end

@implementation ZLExamResultViewController

#pragma mark - 加载数据
- (void)loadData {

    [[YCRequestDataService sharedManager] Post:@"paper/paper/paper_info" parameters:@{@"user_paper_id":_user_paper_id} success:^(id response) {
        if (response[@"error_code"]) {
            NSLog(@"%@",response[@"error_msg"]);
        } else {
            _model = [[ZLExamResultModel alloc] initWithDataDic:response[@"response_data"][@"lists"]];
            
            [self dataWithHeaderView];
            
            [self.myTableView reloadData];
            
        }
    } failure:^{
        NSLog(@"网络或服务器出错");
    }];
    
}

#pragma mark - 头视图数据
- (void)dataWithHeaderView {
    _resultView.model = _model;
}


#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"考试结果";
    
//    [self.view addSubview:self.footerView];
    
    self.myTableView.tableHeaderView = self.resultView;
//    self.myTableView.bounces = NO;
    [self.myTableView registerClass:[ZLExamResultCell class] forCellReuseIdentifier:kZLExamResultCell_ID];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self showBackButtonWithImage:@"exam_back"];
    //重新考试
    [self replyExamCallBack];
    
    [self loadData];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
//    [_footerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.height.mas_equalTo(44);
//        make.left.right.bottom.equalTo(self.view);
//    }];
    
    [self.myTableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.view);
//        make.bottom.equalTo(_footerView.mas_top);
    }];
    
}

#pragma mark - 子视图
- (ZLExamResultView *)resultView {
    if (!_resultView) {
        _resultView = [[ZLExamResultView alloc] initWithFrame:(CGRect){0,0,kSCREEN_WIDTH,kSCREEN_WIDTH}];
    }
    return _resultView;
}

- (UIView *)footerView {
    if (!_footerView) {
        _footerView = [[UIView alloc] init];
        _footerView.backgroundColor = kMY_BLUE_COLOR;
        
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn1.frame = (CGRect){0,0,(kSCREEN_WIDTH-0.5)/2,44};
        [btn1 setTitle:@"错题解析" forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(footerViewClick:) forControlEvents:UIControlEventTouchUpInside];
        btn1.titleLabel.font = kMyFont(15);
        [_footerView addSubview:btn1];
        
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        btn2.frame = (CGRect){(kSCREEN_WIDTH-0.5)/2+0.5,0,(kSCREEN_WIDTH-0.5)/2,44};
        [btn2 setTitle:@"全部解析" forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(footerViewClick:) forControlEvents:UIControlEventTouchUpInside];
        btn2.titleLabel.font = kMyFont(15);
        [_footerView addSubview:btn2];
        
        UIView *lineView = [[UIView alloc] init];
        lineView.frame = (CGRect){(kSCREEN_WIDTH-0.5)/2, 0, 0.5,44};
        lineView.backgroundColor = [UIColor whiteColor];
        [_footerView addSubview:lineView];
        
    }
    return _footerView;
}

#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (_model.answers) {
        return _model.answers.count;
    } else {
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZLExamResultCell *cell = [tableView dequeueReusableCellWithIdentifier:kZLExamResultCell_ID forIndexPath:indexPath];
    
    if (_model.answers) {
        cell.models = _model.answers[indexPath.section];
    }
        
    return cell;
}


#pragma mark - tableview delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_model.answers) {
        CGFloat a = _model.answers[indexPath.section].count/5.;
        NSInteger b = _model.answers[indexPath.section].count/5;
        NSInteger rows = a>b? b+1: b;
        return rows*(kSCREEN_WIDTH/5);
    }
    return .0001f;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"单选题";
    } else {
        return @"多选题";
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

#pragma mark - 点击事件
- (void)footerViewClick:(UIButton *)sender {
    
    if (_model.answers > 0) {
        ZLWrongParsingViewController *wpVC = [[ZLWrongParsingViewController alloc] init];
        wpVC.navigationItem.title = _model.title;
        wpVC.curriculum_id = _model.curriculum_id;
        if ([sender.titleLabel.text isEqualToString:@"错题解析"]) {
            kCX_LOG(@"错题解析");
            
            if ([_model.wrongAnswes[0] count] > 0 || [_model.wrongAnswes[1] count] > 1) {
                wpVC.answersDataArr = _model.wrongAnswes;
            } else {
                wpVC.answersDataArr = _model.answers;
            }
            
        } else {
            kCX_LOG(@"全部解析");
            wpVC.answersDataArr = _model.answers;
        }
        [self.navigationController pushViewController:wpVC animated:YES];
    }
}

- (void)replyExamCallBack {
    __weak typeof(self) weakSelf = self;
    [_resultView setReplyExamCallBack:^{
        kCX_LOG(@"重新考试");
        //TODO:
        ZLExamBeginViewController *examVC = [[ZLExamBeginViewController alloc] init];
        examVC.curriculum_id = weakSelf.curriculum_id;
        examVC.actionStyle = ActionStyleBegin;
        [weakSelf.navigationController pushViewController:examVC animated:YES];
    }];

}
#pragma mark - 返回
- (void)backBarButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{}];
}
#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
